<template>
  <page-container :scrollY="false">
    <search-form
      v-model="searchForm"
      :searchColumns="searchColumns"
      :searchOptions="{ menuSpan: 5 }"
      @on-search="handleSearch">
      <template v-slot:newBtn>
        <el-button
          type="success"
          @click="handleExport"
          >导出</el-button
        >
      </template>
    </search-form>
    <el-row style="height: 100%; width: 100%">
      <el-col
        style="height: 100%"
        :span="device === 'mobile' ? 24 : 12">
        <el-row style="height: 100%; width: 100%">
          <el-col style="height: 50%">
            <RegionPiechart
              :topList="pieUserList"
              :countryList="countryList"></RegionPiechart
          ></el-col>
          <el-col style="height: 50%">
            <SalePiechart
              :topList="piePayList"
              :countryList="countryList"></SalePiechart
          ></el-col>
        </el-row>
      </el-col>
      <el-col
        style="height: 99%"
        :span="device === 'mobile' ? 24 : 12">
        <avue-table
          :dataList="dataList"
          :loading="loading"
          :page="page"
          :tableColumns="tableColumns"
          :tableOptions="{ menu: false }"
          @onPageChange="onPageChange">
          <template v-slot:menu="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="handleView(row)">
            </el-button>
          </template>
        </avue-table>
      </el-col>
    </el-row>
  </page-container>
</template>

<script>
import tableMixins from '@/mixins/table'
import { deepClone } from '@/utils'
import { mapGetters } from 'vuex'
import { searchColumns, tableColumns } from './config/table-config'
import { queryUserRedion } from '@/api/userManage'
import SalePiechart from './components/sale-piechart.vue'
import RegionPiechart from './components/region-piechart.vue'

export default {
  components: { SalePiechart, RegionPiechart },
  mixins: [tableMixins],
  data() {
    return {
      actionType: 'add',
      piePayList: [],
      pieUserList: [],
      dataList: [],
      childList: [],
      searchForm: {
        dateRange: [this.$startStamp(new Date()), this.$endStamp(new Date())],
      },
      editForm: {},
    }
  },
  computed: {
    ...mapGetters(['commonData', 'device', 'countryList']),
    userList() {
      return this.commonData?.userList ?? []
    },
    searchColumns,
    tableColumns,
  },
  methods: {
    async getList() {
      this.dataList = []
      const searchParams = this.formatSearch()
      const queryParams = {
        ...searchParams,
        page: this.page.currentPage,
        page_size: this.page.pageSize,
      }
      this.loading = true

      const { data } = await queryUserRedion(queryParams)
      this.dataList = data?.list ?? []
      this.piePayList = data?.pie?.pay ?? []
      this.pieUserList = data?.pie?.user ?? []
      this.page.total = data?.total ?? 0
      this.summaryData = data ?? {}

      this.loading = false
    },
    // 查询参数格式化
    formatSearch() {
      const searchParams = deepClone(this.searchForm)
      const [start, end] = this.searchForm.dateRange
      searchParams.start = start && this.$dayjs(this.$startStamp(start)).unix()
      searchParams.end = end && this.$dayjs(this.$endStamp(end)).unix()
      delete searchParams.dateRange
      return searchParams
    },
    // 点击导出
    async handleExport() {
      const allData = await this.queryAllData(this.page.total)

      const colums = this.tableColumns.filter(item => {
        return item.prop !== 'Id'
      })

      const originList = deepClone(allData)
      const exportDataList = originList.map((item, index) => {
        Object.entries(item).forEach(([key, value]) => {
          if (key === '$index') {
            item[key] = index + 1
          }
          if (key === 'register_time') {
            item[key] = this.$formatTime(value)
          }
          if (key === 'pay_user_count') {
            item[key] = String(
              `${
                isFinite(item?.pay_user_count / item?.user_count)
                  ? 0
                  : (item?.pay_user_count / item?.user_count) * 100
              }%`,
            )
          }
        })
        return item
      })

      this.$Export.excel({
        title: '用户地区分布数据',
        columns: colums,
        data: exportDataList,
      })
    },
    async queryAllData(pageSize) {
      this.addDataList = []

      const searchParams = this.formatSearch()
      const queryParams = {
        ...searchParams,
        page: this.page.currentPage,
        page_size: pageSize,
      }

      const { data } = await queryUserRedion(queryParams)

      return data?.list ?? []
    },
    // 表格操作
    handleView(row) {
      this.$router.push({
        path: '/dashboard/simple_film',
        query: { name: row?.film_name, id: row?.film_id },
      })
    },
  },
  mounted() {
    this.handleSearch()
  },
}
</script>

<style lang="scss" scoped></style>
